//
//  KTLOVEComment.m
//  KuaiTu
//
//  Created by iOS开发1 on 2018/10/29.
//  Copyright © 2018年 WeiJiang. All rights reserved.
//

#import "KTLOVEComment.h"

@interface KTLOVEComment ()<UITextViewDelegate>
@property (strong,nonatomic) UIView *bgView;

@end
@implementation KTLOVEComment
#pragma mark - ----------   Lifecycle（生命周期） ----------
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configUI];
    }
    
    return self;
}

#pragma mark - ---------- Private Methods（私有方法） ----------
#pragma mark config control（布局控件）
- (void)configUI {
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelClickAction)];
    [self addGestureRecognizer:tap];
    
    self.bgView = [UIView new];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.frame = CGRectMake(0, SCREEN_HEIGHT-aufont(338), SCREEN_WIDTH, aufont(338));
    [self addSubview:self.bgView];
 
    



    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];


    leftButton.frame = CGRectMake(aufont(10), aufont(20),aufont(25), aufont(20));
    [leftButton setImage:[UIImage imageNamed:@"close_btn"] forState:UIControlStateNormal];
    [self.bgView addSubview:leftButton];
    [leftButton addTarget:self action:@selector(cancelClickAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(SCREEN_WIDTH-aufont(65), aufont(20),aufont(60), aufont(27));
    [rightButton setImage:[UIImage imageNamed:@"faBu_item"] forState:UIControlStateNormal];
    [self.bgView addSubview:rightButton];
    [rightButton addTarget:self action:@selector(determineAction) forControlEvents:UIControlEventTouchUpInside];
  
    
    
    
    
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(aufont(10), aufont(60), SCREEN_WIDTH-aufont(20), aufont(200))];
    self.textView.delegate = self;
    self.textView.font = FONT(aufont(13));
    [self.bgView addSubview:self.textView];
    
    
    self.breifLabel = [[UILabel alloc]initWithFrame:CGRectMake(aufont(12), aufont(70), SCREEN_WIDTH-aufont(20), aufont(13))];
    self.breifLabel.text = @"写下您的评论吧";
    self.breifLabel.font = FONT(aufont(13));
    self.breifLabel.textColor = RGB(203,207,212);
    [self.bgView addSubview:self.breifLabel];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(aufont(15), aufont(15))];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bgView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.bgView.layer.mask = maskLayer;
    self.hidden = YES;
    [APP_DELEGATE.window addSubview:self];
}
//正在改变
- (void)textViewDidChange:(UITextView *)textView {
    self.breifLabel.hidden = YES;
    
    
    //字数限制操作
    if (textView.text.length >= 300) {
        textView.text = [textView.text substringToIndex:300];
        
        
    }
    //取消按钮点击权限，并显示提示文字
    if (textView.text.length == 0) {
        self.breifLabel.hidden = NO;
    }
    
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    [UIView animateWithDuration:0.3 animations:^{
        
      self.bgView.frame = CGRectMake(0,aufont(100), SCREEN_WIDTH, aufont(338));
       
        
    } completion:^(BOOL finished) {
        

    }];
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    [UIView animateWithDuration:0.3 animations:^{
        
        self.bgView.frame = CGRectMake(0, SCREEN_HEIGHT-aufont(338), SCREEN_WIDTH, aufont(338));;
        
        
    } completion:^(BOOL finished) {
        
        
    }];
}
#pragma mark actions （点击事件）
-(void)showView{
    self.hidden = NO;
}
                                                 
-(void)cancelClickAction{
    self.hidden = YES;
    [self endeit];
}
- (void)determineAction {
                                                     
    self.determineClickBlock(self.textView.text);
    [self cancelClickAction];
    self.textView.text = @"";
    self.breifLabel.hidden = NO;
    
}
-(void)endeit{
    [self endEditing:YES];
}
@end
